<template>
  <div>
    <Category :scene="scene"></Category>
    <el-card shadow="hover" style="margin-top: 20px">
      <div v-show="scene == 0">
        <!-- 添加按钮 -->
        <el-button
          type="primary"
          :icon="Plus"
          :disabled="!useCategoryStore.category3Id"
          @click="addSpu"
          v-hidden="'btn.Spu.add'"
          >添加SPU</el-button
        >
        <!-- SPU数据列表 -->
        <el-table style="margin: 20px 0" border :data="spuList.records">
          <el-table-column type="index" label="序号" width="80px" />
          <el-table-column prop="spuName" label="SPU名称" />
          <el-table-column
            prop="description"
            label="SPU描述"
            resizable
            show-overflow-tooltip
          />
          <el-table-column label="操作">
            <template v-slot="{ row, $index }">
              <el-button
                type="primary"
                :icon="Plus"
                size="small"
                @click="addSkuHandler(row)"
                v-hidden="'btn.Spu.addsku'"
              ></el-button>
              <el-button
                type="primary"
                :icon="Edit"
                size="small"
                @click="updateSpu(row)"
                v-hidden="'btn.Spu.update'"
              ></el-button>
              <el-button
                type="info"
                :icon="InfoFilled"
                size="small"
                @click="getSkuList(row)"
                v-hidden="'btn.Spu.skus'"
              ></el-button>
              <el-button
                type="danger"
                :icon="Delete"
                size="small"
                @click="deleteSpu(row)"
                v-hidden="'btn.Spu.delete'"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:currentPage="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7]"
          background
          layout="prev, pager, next, jumper, ->, sizes, total"
          :total="spuList.total"
          @current-change="getSpuList"
          @size-change="sizeChangeHandler"
        />
      </div>
      <AddOrUpdateSpu
        v-show="scene == 1"
        @changeScene="changeScene"
        ref="addOrUpdateSpuRef"
      ></AddOrUpdateSpu>
      <AddSku
        v-show="scene == 2"
        ref="addSkuRef"
        @changeScene="changeScene"
      ></AddSku>

      <!-- dialog对话框 -->
      <el-dialog v-model="dialogVisible" title="SKU列表">
        <el-table :data="skuList">
          <el-table-column prop="skuName" label="名称" />
          <el-table-column prop="price" label="价格" />
          <el-table-column prop="weight" label="重量" />
          <el-table-column label="默认图片">
            <template #="{ row, $index }">
              <img
                :src="row.skuDefaultImg"
                style="width: 100px; height: 100px"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { Plus, Edit, Delete, InfoFilled } from "@element-plus/icons-vue";
import { ref, watch, reactive, onUnmounted } from "vue";
import { cloneDeep } from "lodash";
import { ElMessage } from "element-plus";

import categoryStore from "@/stores/category"; // store仓库
import { reqSpuList, reqSkuList, reqDeleteSpu } from "@/api/product/Spu"; // 请求函数
import type { ISpu, ISpuList, IScene, ISkuList } from "@/api/product/Spu/model"; // 数据类型
import AddOrUpdateSpu from "./AddOrUpdateSpu/index.vue";
import AddSku from "./AddSku/index.vue";

const useCategoryStore = categoryStore(); // store仓库对象
const scene = ref<number>(0); // 场景切换及分类列表的显示与隐藏
const currentPage = ref<number>(1); // 当前页码
const pageSize = ref<number>(3); // 每页显示条数
const addOrUpdateSpuRef = ref(); // addOrUpdateSpuRef组件实例
const addSkuRef = ref(); // addSku组件实例
const dialogVisible = ref<boolean>(false); // dialog对话框的状态
const skuList = ref<ISkuList>([]); // sku数据列表

// SPU数据列表
const spuList = reactive<ISpuList>({
  records: [],
  total: 0,
});

// 监听三级分类id, 发生变化时获取SPU数据列表
watch(
  () => useCategoryStore.category3Id,
  () => {
    if (useCategoryStore.category3Id) {
      getSpuList();
    }
  }
);

// 获取SPU数据列表 及 改变当前页时获取数据
const getSpuList = async (page: number = 1) => {
  currentPage.value = page;

  const result = await reqSpuList(
    currentPage.value,
    pageSize.value,
    useCategoryStore.category3Id
  );
  Object.assign(spuList, cloneDeep(result));
};

// 变化每页显示条数时触发的回调
const sizeChangeHandler = (limit: number) => {
  pageSize.value = limit;
  getSpuList();
};

// 添加SPU
const addSpu = () => {
  addOrUpdateSpuRef.value.clearSpuData();
  scene.value = 1;
  addOrUpdateSpuRef.value.addSpuHandler(useCategoryStore.category3Id);
};

// 修改SPU
const updateSpu = (row: ISpu) => {
  scene.value = 1;
  addOrUpdateSpuRef.value.getSpuData(row);
};

// 场景切换
const changeScene = ({ cutScene, flag }: IScene) => {
  scene.value = cutScene;
  getSpuList(flag ? currentPage.value : 1);
};

// 添加sku按钮的回调
const addSkuHandler = (row: ISpu) => {
  addSkuRef.value.removeAddSkuData();
  scene.value = 2;
  addSkuRef.value.getAddSkuData({
    spu: row,
    category1Id: useCategoryStore.category1Id,
    category2Id: useCategoryStore.category2Id,
  });
};

// 查看sku数据按钮的回调
const getSkuList = async (row: ISpu) => {
  const result = await reqSkuList(row.id);
  skuList.value = result;
  dialogVisible.value = true;
};

// 删除SPU
const deleteSpu = async (row: ISpu) => {
  try {
    await reqDeleteSpu(row.id);
    ElMessage({
      type: "success",
      message: "删除成功",
    });
    getSpuList(currentPage.value);
  } catch (error) {
    ElMessage({
      type: "error",
      message: "删除失败",
    });
  }
};

onUnmounted(() => {
  useCategoryStore.deleteCategoryData();
});
</script>

<style scoped></style>
